
import 'package:appflutterhomeschool/model/system_questions_model.dart';
import 'package:appflutterhomeschool/provider/provider_widget.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/ui/widgets/button_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:appflutterhomeschool/utils/letter_display_utils.dart';
import 'package:appflutterhomeschool/view_model/academic_sentiment/answer_display_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

///用户： ninetailedfox
///时间： 2020/7/28
///邮箱： 1037438704@qq.com
///功能： 答题卡-》答题展示页面
class AnswerDisplayPage extends StatefulWidget {
  final int pageTag;
  final String id;

  AnswerDisplayPage({this.pageTag, this.id});

  @override
  _AnswerDisplayPageState createState() => _AnswerDisplayPageState();
}

class _AnswerDisplayPageState extends State<AnswerDisplayPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<AnswerDisplayViewModel>(
      onModelReady: (value) => value.initView(),
      model: AnswerDisplayViewModel(context, widget.id, widget.pageTag),
      builder: (context, viewModel, child) {
        SystemQuestionsModel systemQuestionsModel;
        if (viewModel?.systemQuestions != null) {
          systemQuestionsModel =
              viewModel?.systemQuestions[viewModel?.pageTage];
        }
        return systemQuestionsModel == null
            ? Center(
                child: CircularProgressIndicator(backgroundColor: Colors.blue))
            : Scaffold(
                appBar: MyAppBar2(
                  title: "作业详情",
                  rightIcon: Container(
                    padding: EdgeInsets.only(right: 10),
                    alignment: Alignment.center,
                    child: Row(
                      children: [
                        Text("${viewModel.pageTage + 1}",
                            textAlign: TextAlign.center,
                            style: TextStyles.textBlue30),
                        Text("/${viewModel?.systemQuestions?.length ?? 0}",
                            textAlign: TextAlign.center,
                            style: TextStyles.textDark30),
                      ],
                    ),
                  ),
                ),
                body: Stack(
                  children: [
                    Positioned(
                      top: 0,
                      left: 0,
                      right: 0,
                      bottom: ScreenUtil().setWidth(115),
                      child: viewModel.systemQuestions == null
                          ? Center(
                              child: CircularProgressIndicator(
                                  backgroundColor: Colors.blue))
                          : SingleChildScrollView(
                              child: Column(
                                children: [
                                  Container(
                                    padding: EdgeInsets.only(
                                        top: ScreenUtil().setHeight(35),
                                        left: ScreenUtil().setHeight(35),
                                        right: ScreenUtil().setHeight(35)),
                                    child: Text(
                                        "${systemQuestionsModel?.getStringType() ?? ""}${systemQuestionsModel?.questionBody ?? ""}"),
                                  ),
                                  systemQuestionsModel.questionType == "C"
                                      ? Container(
                                          child: Column(
                                            children: [
                                              ListViewWidget(
                                                shanchuFujian: false,
                                                setWidth: 100,
                                                type: 1,
                                                listImage: viewModel
                                                    .systemQuestions[
                                                        viewModel.pageTage]
                                                    .questionImg,
                                                onTapIndex: (index) {},
                                              ),
                                              viewModel.pictureItemWidget(context),
                                              ListViewWidget(
                                                shanchuFujian : true,
                                                listImage: viewModel
                                                    .systemQuestions[
                                                viewModel.pageTage]
                                                    .userImage,
                                              )
                                            ],
                                          ),
                                        )
                                      : ListView.builder(
                                          physics:
                                              NeverScrollableScrollPhysics(),
                                          itemCount: systemQuestionsModel
                                              ?.options.length,
                                          shrinkWrap: true,
                                          itemBuilder: systemQuestionsModel
                                                      .questionType ==
                                                  "SC"
                                              ? _danxuan
                                              : _duoxuan),
                                ],
                              ),
                            ),
                    ),
                    Positioned(
                      left: 0,
                      right: 0,
                      bottom: 0,
                      child: Row(
                        children: [
                          Expanded(
                            child: ButtonWidget().rectangleButtonWidget(
                                buttonName: "上一题",
                                bgColor: Colors.white,
                                textStyle: TextStyles.textDark30,
                                onPressed: viewModel.lastQuestionOnTap),
                          ),
                          Expanded(
                            child: ButtonWidget().rectangleButtonWidget(
                              buttonName: viewModel.buttonName,
                              bgColor: Colors.white,
                              textStyle: TextStyles.textDark30,
                              onPressed: viewModel.nextQuestionOnTap,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              );
      },
    );
  }

  Widget _danxuan(context, index) {
    AnswerDisplayViewModel answerDisplayViewModel = Provider.of(context);
    Options options = answerDisplayViewModel
        .systemQuestions[answerDisplayViewModel.pageTage]?.options[index];
    return GestureDetector(
      child: Container(
        child: Row(
          children: [
            Container(
              margin: EdgeInsets.all(ScreenUtil().setHeight(35)),
              alignment: Alignment.center,
              width: ScreenUtil().setWidth(80),
              height: ScreenUtil().setWidth(80),
              decoration: options.isCorrect
                  ? BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.all(
                          Radius.circular(ScreenUtil().setWidth(80 / 2))))
                  : BoxDecoration(
                      color: Colors.white,
                      border: Border.all(color: Colors.blue, width: 0.5),
                      borderRadius: BorderRadius.all(
                          Radius.circular(ScreenUtil().setWidth(80 / 2))),
                    ),
              child: Text(
                LetterDisplayUtils.getIndexLetter(index),
                style: options.isCorrect
                    ? TextStyles.textWhite37
                    : TextStyles.textBlue37,
              ),
            ),
            Text(
              options?.body ?? "",
              style: TextStyles.textContent37,
            ),
          ],
        ),
      ),
      onTap: () => answerDisplayViewModel.choiceOnTap(index),
    );
  }

  Widget _duoxuan(context, index) {
    AnswerDisplayViewModel answerDisplayViewModel = Provider.of(context);
    Options options = answerDisplayViewModel
        .systemQuestions[answerDisplayViewModel.pageTage]?.options[index];
    return GestureDetector(
      child: Container(
        child: Row(
          children: [
            Container(
              margin: EdgeInsets.all(ScreenUtil().setHeight(35)),
              alignment: Alignment.center,
              width: ScreenUtil().setWidth(80),
              height: ScreenUtil().setWidth(80),
              decoration: options.isCorrect
                  ? BoxDecoration(
                      color: Colors.blue,
                    )
                  : BoxDecoration(
                      color: Colors.white,
                      border: Border.all(color: Colors.blue, width: 0.5),
                    ),
              child: Text(
                options?.order,
                style: options.isCorrect
                    ? TextStyles.textWhite37
                    : TextStyles.textBlue37,
              ),
            ),
            Text(
              options?.body ?? "",
              style: TextStyles.textContent37,
            ),
          ],
        ),
      ),
      onTap: () => answerDisplayViewModel.choiceOnTap(index),
    );
  }



}
